package com.csun.cmny.provider.controller;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.pojo.CacBaseMinorItem;
import com.csun.cmny.provider.service.CacBaseMinorItemService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/*
 * 评估小项管理——基础小项
 * */
@RestController
@RequestMapping("/CacBaseMinorItem")
public class CacBaseMinorItemController extends BaseController {
    @Autowired
    private CacBaseMinorItemService cacBaseMinorItemService;

    //添加单个小项
    @RequestMapping(value = "/addCacBaseMinorItem", method = RequestMethod.POST)
    public Wrapper<CacBaseMinorItem> addCacBaseMinorItem(CacBaseMinorItem cacBaseMinorItem) {
        //校验
        if (cacBaseMinorItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项名称不能为空", null);
        }
        if (cacBaseMinorItem.getBigItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        cacBaseMinorItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织机构ID
        CacBaseMinorItem cacBaseMinorItem1 = new CacBaseMinorItem();
        cacBaseMinorItem1.setBigItemId(cacBaseMinorItem.getBigItemId());
        cacBaseMinorItem1.setName(cacBaseMinorItem.getName());
        cacBaseMinorItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织机构ID
        //根据小项名称和大项id判断该小项是否在该大项中已经存在
        CacBaseMinorItem cacBaseMinorItem2 = cacBaseMinorItemService.selectByCmi(cacBaseMinorItem1);
        if (cacBaseMinorItem2 != null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该小项已经在该大项中存在", null);
        }
        /*判断该小项所属大项的类别(type)
         * 0-表示要计算的项，1-表示是类别总分，2-表示是类别的等级
         * 如果type是0，分数一定不为空；如果type是1，分数和描述一定为空；如果type是2，描述一定不为空
         * 此暂时未实现
         * */

        int n = cacBaseMinorItemService.insert(cacBaseMinorItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacBaseMinorItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacBaseMinorItem);
    }

    //根据小项id删除单个小项
    @RequestMapping(value = "/delCacBaseMinorItem", method = RequestMethod.POST)
    public Wrapper delCacBaseMinorItem(Integer minorItemId) {
        if (minorItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        //删除
        int n = cacBaseMinorItemService.delById(minorItemId);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败", n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功", n);
    }

    //修改单个小项
    @RequestMapping(value = "/updateCacBaseMinorItem", method = RequestMethod.POST)
    public Wrapper<CacBaseMinorItem> updateCacBaseMinorItem(CacBaseMinorItem cacBaseMinorItem) {
        if (cacBaseMinorItem.getMinorItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        /*判断该小项所属大项的类别(type)
         * 0-表示要计算的项，1-表示是类别总分，2-表示是类别的等级
         * 如果type是0，分数一定不为空；如果type是1，分数和描述一定为空；如果type是2，描述一定不为空
         * 此暂时未实现
         * */
        cacBaseMinorItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织机构ID
        CacBaseMinorItem cacBaseMinorItem1 = new CacBaseMinorItem();
        cacBaseMinorItem1.setBigItemId(cacBaseMinorItem.getBigItemId());
        cacBaseMinorItem1.setName(cacBaseMinorItem.getName());
        cacBaseMinorItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织机构ID
        //根据小项名称和大项id判断该小项是否在该大项中已经存在
        CacBaseMinorItem cacBaseMinorItem2 = cacBaseMinorItemService.selectByCmi(cacBaseMinorItem1);
        if (cacBaseMinorItem2 != null && !cacBaseMinorItem2.getMinorItemId().equals(cacBaseMinorItem2.getMinorItemId())) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该小项已经在该大项中存在", cacBaseMinorItem2);
        }

        int n = cacBaseMinorItemService.update(cacBaseMinorItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", cacBaseMinorItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", cacBaseMinorItem);
    }

    //查询单个小项
    @RequestMapping(value = "/selectBaseCmiById", method = RequestMethod.POST)
    public Wrapper selectBaseCmiById(Integer minorItemId) {
        if (minorItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        CacBaseMinorItem cacBaseMinorItem = cacBaseMinorItemService.selectCmiById(minorItemId);
        if (cacBaseMinorItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", minorItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacBaseMinorItem);
    }
}
